import { JSDOM } from "jsdom";
import { execSync } from "child_process";

function outputConsoleStr(output){
    const styleArr=[
        'background:green',
        'color:#FFFFFF',
        'font-weight: bold',
        'padding:5px',
        'border:1px solid #fff',
    ]

    return `console.log('%c${output}','${styleArr.join(';')}');`
   
}


export default (userOption={}) => {
    
    const {
        customExtendInfo=null //自定义字段
    }=userOption

    return {
        name: "vite-plugin-mark-version",
        transformIndexHtml(html) {
            const commitInfo = execSync(`git log -1 HEAD --pretty='%cd,%an,%s,%h'`, {
                encoding: "utf-8",
            });
           
            //将html字符串转化为Dom
            const dom = new JSDOM(html);
            const document = dom.window.document;

            //获取Git信息
            const commitInfoList = commitInfo.split(",");
            let infoMeta = document.createElement("meta");

            infoMeta.setAttribute("time", new Date());
            infoMeta.setAttribute("auth", commitInfoList[1]);
            infoMeta.setAttribute("description", commitInfoList[2]);
            infoMeta.setAttribute("hash", commitInfoList[3]);

            if(customExtendInfo){
                for(let key in customExtendInfo){
                    infoMeta.setAttribute(key, customExtendInfo[key]);
                }
            }
            //向HTML中插入信息
            document.head.append(infoMeta)


            //向浏览器控制台输出信息
            let resOutput=''
            const script=document.createElement('script')
            resOutput+=outputConsoleStr(`作者: ${commitInfoList[1]}`)
            resOutput+=outputConsoleStr(`提交msg: ${commitInfoList[2]}`)
            resOutput+=outputConsoleStr(`环境: ${process.env.NODE_ENV}`)
            script.textContent=resOutput
            document.body.append(script)

            return dom.serialize();
        },
    };
};
